package com.meta.nd.vo.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.meta.cache.NdCacheService;
import com.meta.nd.util.SpringBeanUtil;
import com.meta.nd.util.json.NdJsonTool;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;



@Slf4j
public class NvDeserializer extends JsonDeserializer<Nv> {
    @Override
    public Nv deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        JsonNode node = jp.getCodec().readTree(jp);
        String code = null;
        if ( !node.get("__ndCode").isNull() ) {
            code = node.get("__ndCode").asText();
        }

       NdCacheService ndCacheService = SpringBeanUtil.getNdCacheService();
       try {
           Nd nd = ndCacheService.getNd(code);
           Nv nv = NdJsonTool.jsonNode2Nv(nd, node);
           return nv;
       }catch (Exception e){
           log.error(e.getMessage(),e);
           throw e;
       }
    }
}
